class Solution(object):
    def minDifference(self, nums, queries):
        n = len(nums)
        p = [[0 for _ in range(100)] for _ in range(n)]
        for i in range(n):
            if i == 0:
                p[i][nums[i] - 1] += 1
            else:
                for j in range(100):
                    if nums[i] - 1 == j:
                        p[i][j] = p[i - 1][j] + 1
                    else:
                        p[i][j] = p[i - 1][j]
        m = len(queries)
        ans = [-1] * m
        for index, (u, v) in enumerate(queries):
            last = -1
            min_val = float('inf')
            for j in range(100):
                if u == 0:
                    if p[v][j] > 0:
                        if last >= 0:
                            min_val = min(min_val, j - last)
                        last = j
                else:
                    if p[v][j] - p[u - 1][j] > 0:
                        if last >= 0:
                            min_val = min(min_val, j - last)
                        last = j
            if min_val != float('inf'):
                ans[index] = min_val
        return ans

